﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace MySSH.Common.Helper
{
    public static class JSONHelper
    {
        public static string ToJSON(object obj)
        {
            return JsonConvert.SerializeObject(obj);
        }
        /// <summary>
        /// 转换JSON
        /// </summary>
        /// <param name="obj">需要转换的对象</param>
        /// <param name="dateFormat">日期处理格式 如：yyyy'-'MM'-'dd' 'HH':'mm':'ss</param>
        /// <returns></returns>
        public static string ToJSON(object obj, string dateFormat)
        {
            if (string.IsNullOrEmpty(dateFormat))
                return JSONHelper.ToJSON(obj);

            IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
            timeConverter.DateTimeFormat = dateFormat;
            return JsonConvert.SerializeObject(obj, timeConverter);
        }

        public static string Error()
        {
            return Error("");
        }

        public static string Error(string msg)
        {
            return ToJSON(new { success = false, msg = msg });
        }

        public static string Success( )
        {
            return Success("");
        }
        public static string Success(string msg)
        {
            return ToJSON(new { success = true, msg = msg });
        }

        public static string ToJSON(object obj, int totalCount)
        {
            return ToJSON(new { success = true, totalCount = totalCount, data = obj });
        }

    }
}
